package com.sxz.sbs.module.client;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.adapter.BuildingAdapter;
import com.sxz.sbs.module.client.bean.BuildingBean;
import com.sxz.sbs.module.client.presenter.BuildingPresenter;
import com.sxz.sbs.module.client.view.BuildingView;
import com.sxz.sbs.module.staff.OperateSuccessActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
/**
 * @author hp
 * @date 2018/1/10
 * 小区楼号
 */

public class BuildingActivity extends BaseActivity<BuildingPresenter> implements BuildingView {

    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.list_building)
    RecyclerView listBuilding;
    @BindView(R.id.tv_allot)
    TextView tvAllot;
    @BindView(R.id.tv_calloff)
    TextView tvCalloff;
    @BindView(R.id.iv_title_back)
    ImageView imgBack;
    private List<BuildingBean.ListBean> buildingBean=new ArrayList<>();
    private BuildingAdapter myAdapter;
    private static boolean isShow;
    private boolean isSelect = false;
    public boolean mIsLive = true;
    private int currentPage = 1;
    private List<String> list=new ArrayList<>();
    private int parkingId;
    private int mangerId;
    private boolean isList = true;
    @Override
    protected void init() {
        parkingId = getIntent().getIntExtra("parkingid",0);
        mangerId = getIntent().getIntExtra("mangerId",0);
        getData();
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setRefreshing(true);
        myAdapter = new BuildingAdapter(buildingBean);
        listBuilding.setLayoutManager(new LinearLayoutManager(this));
        listBuilding.setAdapter(myAdapter);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                isSelect = false;
                getData();
                list.clear();
            }
        });

        myAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage ++;
                getData();
            }
        },listBuilding);

        listBuilding.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                    if (buildingBean.get(position).isChecked()) {
                        buildingBean.get(position).setIsContains(2);
                        list.remove(buildingBean.get(position).getId()+"");
                    } else {
                        buildingBean.get(position).setIsContains(1);
                        list.add(buildingBean.get(position).getId()+"");
                    }
                    myAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_building;
    }

    @Override
    protected BuildingPresenter createPresenter() {
        return new BuildingPresenter();
    }


    @OnClick({R.id.tv_allot, R.id.tv_calloff, R.id.iv_title_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_allot:
                String str = "";
                for (int i = 0; i < list.size(); i++) {
                    str = str +list.get(i)+",";
                }
                Log.e("数据>>",str);
                if (!str.equals("")) {
                    str = str.substring(0,str.length()-1);
                    getSubmit(str);
                }else {
                    ToastUtils.showShort("请选择楼号");
                }
                break;
            case R.id.tv_calloff:
                finish();
                break;
            case R.id.iv_title_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        isShow = false;
        myAdapter.notifyDataSetChanged();
        super.onDestroy();

    }

    @Override
    public void onClientListSuccess(BuildingBean data) {
        if (mIsLive && data != null) {
            refreshLayout.setRefreshing(false);
            if (data.getList() != null && data.getList().size() >0) {
                stateLayout.showContent();
                /**
                 * 把之前人员负责的楼栋添加到数组里
                 */
                for (int i=0;i<data.getList().size();i++){
                    if (data.getList().get(i).getIsContains()==1){
                        list.add(data.getList().get(i).getId()+"");
                    }
                }
                if (currentPage == 1) {
                    buildingBean.clear();
                    buildingBean.addAll(data.getList());
                    myAdapter.notifyDataSetChanged();

                } else {
                    buildingBean.addAll(data.getList());
                    myAdapter.notifyDataSetChanged();
                }
                myAdapter.loadMoreComplete();
            } else {
                if (currentPage == 1) {
                    buildingBean.clear();
                    myAdapter.notifyDataSetChanged();
                    //  数据为空
                    stateLayout.showEmpty();
                } else {
                    myAdapter.loadMoreEnd();
                }
            }
        }
    }

    @Override
    public void onSubmitSuccess(String str) {
        startActivity(OperateSuccessActivity.class);
        finish();
    }

    private void getData() {
        isList = true;
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        map.put("parkingId", parkingId);
        map.put("pageNumber", currentPage);
        map.put("pageSize", AppConstant.PAGE_SIZE);
        map.put("managerId",mangerId);
        mPresenter.getBuildingList(map);
    }

    @Override
    public void showError(int errorTag, String msg) {
        refreshLayout.setRefreshing(false);
        if (isList) {
            if (currentPage == 1) {
                if (errorTag == AppConstant.NET_ERROR_CODE) {
                    stateLayout.showNetwork();
                } else {
                    stateLayout.showEmpty();
                }
            } else {
                myAdapter.loadMoreFail();
            }

        } else {
            super.showError(errorTag, msg);
        }

    }

    /**
     * 提交请求
     */
    private void getSubmit(String buildId){
        isList = false;
        /**
          managerId	int	是	员工Id
         buildId	string	是	楼号Id 格式：1,2,3
         */
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        map.put("managerId", mangerId);
        map.put("buildId", buildId);
        mPresenter.getSubmit(map);
    }
}
